package com.evan.review.architecture.components;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.Transformations;

import com.evan.review.R;

/**
 * LiveData示例Activity
 * 展示LiveData的基本用法和特性
 */
public class LiveDataExampleActivity extends AppCompatActivity {
    
    private TextView tvValue;
    private TextView tvTransformedValue;
    private TextView tvStatus;
    
    // 创建MutableLiveData实例，初始值为0
    private MutableLiveData<Integer> counterLiveData = new MutableLiveData<>(0);
    
    // 使用Transformations.map转换LiveData
    private LiveData<String> transformedLiveData = Transformations.map(counterLiveData, 
            input -> "转换后的值: " + (input * 10));
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_live_data_example);
        
        // 设置标题
        setTitle("LiveData示例");
        
        // 初始化视图
        initViews();
        
        // 设置LiveData观察者
        setupObservers();
    }
    
    private void initViews() {
        tvValue = findViewById(R.id.tv_value);
        tvTransformedValue = findViewById(R.id.tv_transformed_value);
        tvStatus = findViewById(R.id.tv_status);
        
        // 增加按钮
        Button btnIncrease = findViewById(R.id.btn_increase);
        btnIncrease.setOnClickListener(v -> {
            // 获取当前值并增加1
            Integer currentValue = counterLiveData.getValue();
            counterLiveData.setValue(currentValue != null ? currentValue + 1 : 1);
            updateStatus("setValue() 更新 - UI线程");
        });
        
        // 减少按钮
        Button btnDecrease = findViewById(R.id.btn_decrease);
        btnDecrease.setOnClickListener(v -> {
            // 获取当前值并减少1
            Integer currentValue = counterLiveData.getValue();
            counterLiveData.setValue(currentValue != null ? currentValue - 1 : -1);
            updateStatus("setValue() 更新 - UI线程");
        });
        
        // 异步更新按钮
        Button btnAsyncUpdate = findViewById(R.id.btn_async_update);
        btnAsyncUpdate.setOnClickListener(v -> {
            // 在后台线程中更新LiveData
            new Thread(() -> {
                try {
                    Thread.sleep(1000); // 模拟耗时操作
                    Integer currentValue = counterLiveData.getValue();
                    counterLiveData.postValue(currentValue != null ? currentValue + 5 : 5);
                    
                    // 更新状态文本需要在UI线程
                    runOnUiThread(() -> updateStatus("postValue() 更新 - 后台线程"));
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }).start();
        });
        
        // 重置按钮
        Button btnReset = findViewById(R.id.btn_reset);
        btnReset.setOnClickListener(v -> {
            counterLiveData.setValue(0);
            updateStatus("重置为0");
        });
    }
    
    private void setupObservers() {
        // 观察原始LiveData
        counterLiveData.observe(this, new Observer<Integer>() {
            @Override
            public void onChanged(Integer value) {
                tvValue.setText("当前值: " + value);
            }
        });
        
        // 观察转换后的LiveData
        transformedLiveData.observe(this, new Observer<String>() {
            @Override
            public void onChanged(String transformedValue) {
                tvTransformedValue.setText(transformedValue);
            }
        });
    }
    
    private void updateStatus(String status) {
        tvStatus.setText("最近操作: " + status);
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        // LiveData会自动处理生命周期，无需手动移除观察者
        tvStatus.setText("Activity销毁，LiveData自动移除观察者");
    }
} 